package jee.boot.module.jasper.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jee.boot.common.basic.BaseController;
import jee.boot.module.jasper.iface.JasperQueryDataInterface;
import jee.boot.module.jasper.service.JasperReportsService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/jasperreports")
public class JasperReportController extends BaseController {

	private final static String JASPER = ".jasper";

	@Autowired
	private JasperReportsService jasperReportsService;

	//前端查询数据，数据量少的话，可以直接由前端传过来的数据直接生成PDF。
	@SuppressWarnings("deprecation")
	@RequestMapping("/{reportName}")
	public void reportsData(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("reportName") final String reportName, @RequestParam String jasperDatas) {
		JSONArray jsonArray = JSONArray.fromObject(jasperDatas);  
		jasperReportsService.exportPdf(reportName + JASPER, true, response, JSONArray.toList(jsonArray,Map.class));
	}
	//把资源链接传给Jasper，调用Jasper内部sql查询，可以外部传入参数
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/japserSelf/{reportName}")
	public void reportsDataByJasperSlef(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("reportName") final String reportName, @RequestParam String jasperParams) {
		JSONObject jsonObject = JSONObject.fromObject(jasperParams); 
		jasperReportsService.exportPdf(reportName + JASPER, true, response, (Map)jsonObject);
	}
	//controller 重写调用示例
	@GetMapping("/query/{reportName}")
	public void reportsDataByQueryData(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("reportName") final String reportName) {
		jasperReportsService.exportPdf(reportName + JASPER, true, response, new JasperQueryDataInterface<Map<String,Object>>() {

			@Override
			public List<Map<String,Object>> getJasperData() {
				// TODO Auto-generated method stub
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("asdf", "LXDX");
				list.add(map);
				return list;
			}
		});
	}
	//test 测试
	@RequestMapping("/test/test")
	public void test(HttpServletRequest request, HttpServletResponse response) {
		jasperReportsService.exportPdf("test.jasper", true, response, new JasperQueryDataInterface<Map<String,Object>>() {

			@Override
			public List<Map<String,Object>> getJasperData() {
				// TODO Auto-generated method stub
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("asdf", "wolf,where is your weapon！");
				list.add(map);
				return list;
			}
		});
	}
}
